# stories/templatetags/story_extras.py
from django import template
from django.utils.safestring import mark_safe
import re

register = template.Library()

@register.filter
def highlight_word(text, word):
    """
    高亮显示文本中的指定词汇
    """
    if not word:
        return text
    
    # 使用正则表达式查找并高亮词汇
    pattern = re.compile(f'({re.escape(word)})', re.IGNORECASE)
    highlighted_text = pattern.sub(r'<span class="highlight">\1</span>', text)
    
    return mark_safe(highlighted_text)